﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyDuAn.BUS;
using System.Collections;
using System.Data.SqlClient;
using System.Text.RegularExpressions;

namespace QuanLyDuAn.GUI
{
    public partial class FrmVacancy : DevComponents.DotNetBar.Office2007Form
    {
        DataSet ds;
        VacancyController vacancyControl = new VacancyController();
        public int permission;
        public FrmVacancy()
        {
            InitializeComponent();
        }
        public bool CheckNotEmpty()
        {
            String amount = txtAmount.Text;
            String number = "^[0-9]+$";
            Regex regex = new Regex(number);
            bool check;
            if (dtDeadLine.Text.CompareTo("") == 0)
            {
                check = false;
                MessageBox.Show("Please Select Dateline");
            }
            else if (dtRegisterDate.Text.CompareTo("") == 0)
            {
                check = false;
                MessageBox.Show("Please Select Register Date");
            }
            else if (txtID.Text.CompareTo("") == 0)
            {
                check = false;
                MessageBox.Show("ID Vacancy is not Empty! ");
            }
            else if (txtperiod.Text.CompareTo("") == 0)
            {
                check = false;
                MessageBox.Show("Period is not Empty! ");
            }
            else if (txtAmount.Text.CompareTo("") == 0)
            {
                check = false;
                MessageBox.Show("Amount is not Empty! ");
            }
            else if (regex.IsMatch(amount) == false)
            {
                check = false;
                MessageBox.Show("Amount is only a Number! ");
            }
            else
            {
                check = true;
            }
            return check;
        }
        public bool CheckExist()
        {
            bool check;
            String vId = txtID.Text;
            DataTable dt = new DataTable();
            ds = new DataSet();
            ds = vacancyControl.CheckVacancy(vId);
            dt = ds.Tables[0];
            if (dt.Rows.Count != 0)
            {
                check = false;
                MessageBox.Show("Vacancy is Exits !");
            }
            else
            {
                check = true;
            }
            return check;
        }
        public void Permission()
        {
            btnAdd.Enabled = false;
            btnEdit.Enabled = false;
            btnDelete.Enabled = false;
        }
        public void ResetForm()
        {
            txtID.Text = "";
            txtAmount.Text = "";
            txtDescription.Text = "";
            txtperiod.Text = "";
            rdClose.Checked = true;
        }
        public void LoadForm()
        {
            ds = vacancyControl.LoadVacancy();
            dataGridViewX1.DataSource = ds.Tables[0];
            int i = dataGridViewX1.RowCount;
            txtCount.Text = i.ToString();
            DataTable dt = new DataTable();
            dt = ds.Tables[0];
            String value = dt.Rows[1][0].ToString();
        }
        public String SelectStatus()
        {
            String status;
            if (rdOpen.Checked == true)
            {
                status = "Open";
            }
            else if (rdClose.Checked == true)
            {
                status = "Close";
            }
            else
            {
                status = "Wait";
            }
            return status;
        }
        public void InsertVacancy()
        {
            String id = txtID.Text;
            String dateRegister = dtRegisterDate.Value.ToString();
            String dateDeadLine = dtDeadLine.Value.ToString();
            String title = txtperiod.Text;
            String description = txtDescription.Text;
            String a = txtAmount.Text;
            String department = (String)cmbDepartment.SelectedItem;
            String status = this.SelectStatus();
            if ((this.CheckNotEmpty() == true) && (this.CheckExist()==true))
            {
                int amount = Int32.Parse(a);
                vacancyControl.InsertVacancy(id, dateRegister, status, title, description, amount, department, dateDeadLine);
                MessageBox.Show("Insert successful !!!");
                this.LoadForm();
                this.ResetForm();
            }
        }
        private void FrmVacancy_Load(object sender, EventArgs e)
        {
            this.dataGridViewX1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            cmbDepartment.Items.Add("Software I");
            cmbDepartment.Items.Add("Software II");
            cmbDepartment.Items.Add("Software III");
            //rdClose.Checked = true;
            this.LoadForm();
            btnDelete.Enabled = false;
            btnEdit.Enabled = false;
            this.dataGridViewX1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; 
        }
        public void UpdateVacancy()
        {
            String id = txtID.Text;
            String dateRegister = dtRegisterDate.Value.ToString();
            String dateDeadLine = dtDeadLine.Value.ToString();
            String title = txtperiod.Text;
            String description = txtDescription.Text;
            String a = txtAmount.Text;
            String department = (String)cmbDepartment.SelectedItem;
            String status = this.SelectStatus();
            if (this.CheckNotEmpty() == true)
            {
                int amount = Int32.Parse(a);
                vacancyControl.UpdateVacancy(id, dateRegister, status, title, description, amount, department, dateDeadLine);
                MessageBox.Show("Update successful !!!");
                this.LoadForm();
                this.ResetForm();
            }
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            this.InsertVacancy();
        }
      
        private void dataGridViewX1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (permission == 1)
            {
                btnDelete.Enabled = true;
                btnEdit.Enabled = true;
                DataGridViewRow row = new DataGridViewRow();
                row = dataGridViewX1.Rows[e.RowIndex];
                txtID.Text = row.Cells[0].Value.ToString();
                dtRegisterDate.Text = row.Cells[1].Value.ToString();
                //txtTitle.Text = row.Cells[3].Value.ToString();
                txtDescription.Text = row.Cells[4].Value.ToString();
                txtAmount.Text = row.Cells[5].Value.ToString();
                cmbDepartment.SelectedItem = row.Cells[6].Value.ToString();
                dtDeadLine.Text = row.Cells[7].Value.ToString();
                txtID.Enabled = false;
                String status = row.Cells[2].Value.ToString();
                if (status.CompareTo("Open") == 0)
                {
                    rdOpen.Checked = true;
                    rdClose.Checked = false;
                    rdWait.Checked = false;
                }
                else if (status.CompareTo("Close") == 0)
                {
                    rdOpen.Checked = false;
                    rdClose.Checked = true;
                    rdWait.Checked = false;
                }
                else
                {
                    rdOpen.Checked = false;
                    rdClose.Checked = false;
                    rdWait.Checked = true;
                }
            }
            else
            {
                MessageBox.Show("You don't have permission update information");
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            this.UpdateVacancy();
            this.LoadForm();
            this.ResetForm();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            {
                if (MessageBox.Show("You really want to delete?", "Notice", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                {
                    try
                    {
                        String id = txtID.Text;
                        vacancyControl.DeleteVacancy(id);
                        MessageBox.Show("Del successfull", "Notice", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        LoadForm();
                        ResetForm();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Errors");
                    }
                }
            }
        }

        private void dataGridViewX1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }


        

        

      }

      
       
 }

